import time

import allure
from selenium.common import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from page.base_page import BasePage


class CompensationDetail(BasePage):
    __order_management = By.XPATH, "//*[text()='订单管理']"
    __proxy_order = By.XPATH, "//*[text()='代理订单']"
    __proxy_order_list = By.XPATH, "//*[@href = '/order/proxyOrder/proxyOrderApply']"
    __repayment_statistics = By.XPATH, '//*[@href="/statistics/proxyReport/repayStatisticsProxy"]'
    __in_stock_statistics = By.XPATH, '//*[@href="/statistics/proxyReport/outstandingStatisticsProxy"]'
    __overdue_statistics = By.XPATH, '//*[@href="/statistics/proxyReport/overdueStatisticsProxy"]'
    __search_button = By.XPATH, "//*[text()='搜索']/.."

    def goto_proxy_order(self):
        from page.proxy_order_apply_page import ProxyOrderApply
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击代理订单'):
            self.find_and_click(self.__proxy_order)
        with allure.step('点击订单列表'):
            self.find_and_click(self.__proxy_order_list)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return ProxyOrderApply(self.driver)

    def goto_repayment_statistics(self):
        from page.repayment_statistics_page import RepaymentStatistics
        with allure.step("点击还款统计报表"):
            self.find_and_click(self.__repayment_statistics)
        return RepaymentStatistics(self.driver)

    def goto_in_stock_statistics(self):
        from page.in_stock_statistics_page import InStockStatistics
        with allure.step("点击在库统计"):
            self.find_and_click(self.__in_stock_statistics)
        return InStockStatistics(self.driver)

    def goto_overdue_statistics(self):
        from page.overdue_statistics_page import OverdueStatistics
        with allure.step('点击逾期统计'):
            self.find_and_click(self.__overdue_statistics)
        return OverdueStatistics(self.driver)

    def get_compensation_amount_summary(self):
        with allure.step("获取代偿汇总数据"):
            time.sleep(0.5)
            while True:
                try:
                    compensation_summary = []
                    for i in range(1, 5):
                        compensation_summary.append(
                            (self.find(By.XPATH, f"//*[@class='el-row']/div[{i}]/div").text.split(':'))[1])
                    break
                except (StaleElementReferenceException, IndexError):
                    pass
        return compensation_summary

    def get_first_compensation_details(self):
        with allure.step("获取第一条代偿记录"):
            compensation_details = []
            try:
                self.wait(EC.presence_of_element_located((By.XPATH, "//*[@class = 'el-table__body']/tbody/tr[1]")))
                for i in range(4, 14):
                    compensation_details.append(
                        self.find(By.XPATH, f"//*[@class = 'el-table__body']/tbody/tr[1]/td[{i}]").text)
            except StaleElementReferenceException:
                pass
        return compensation_details

    def click_search_button(self):
        with allure.step("点击搜索按钮"):
            self.find_and_click(self.__search_button)
        return CompensationDetail(self.driver)
